package com.example.ai.controller;


import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@CrossOrigin(origins = "*")
@RestController
public class OpenAIChatController {
    private final OpenAiChatModel chatModel;

    @Autowired
    public OpenAIChatController(OpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/chat/generate")
    public Map<String,String> generate(@RequestParam(value = "message", defaultValue = "请介绍下你自己") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping(value = "/ai/chat/generateStream",produces = "text/plain;charset=utf-8")
    //@GetMapping(value = "/ai/generateStream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "讲一个笑话") String message) {
        //Prompt prompt = new Prompt(new UserMessage(message));//使用提示词生成prompt
        //return this.chatModel.stream(prompt);
        Flux<String> stream = chatModel.stream(message);
        return stream;

    }
}
